package com.easepal7506a.project.ui.fragment;

import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewStub;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TextView;

import com.easepal7506a.project.Constant.CommmandNum;
import com.easepal7506a.project.R;
import com.easepal7506a.project.manager.DataManager;
import com.easepal7506a.project.observer.CloseAdvance;
import com.easepal7506a.project.ui.iview.IFraAirView7506;
import com.easepal7506a.project.ui.presenter.FraAirPresenter7506;
import com.easepal7506a.project.view.MoveLayout;
import com.easepal7506a.project.widget.LayoutBody7506;
import com.easepal7506a.project.widget.LayoutBody7506L;
import com.easepal7506a.project.widget.LayoutFunctionView7506;
import com.easepal7506a.project.widget.LayoutGears5_7506;
import com.example.reslib.utils.PreferenceWrapper;
import com.ogawa.base.utils.CommonUtil;
import com.ogawa.base.utils.DownTimer;

import java.util.ArrayList;
import java.util.List;


/**
 * Created by Fhb on 2018/3/29.
 */
public class AirPressureMassageFragment extends BaseTimeTaskFragment implements View.OnClickListener, MoveLayout.OnChooseAction, IFraAirView7506 {

    private View inflate;

    private LinearLayout mLlAirPosition;

    private LayoutBody7506 mLayoutBody7506;
    private LayoutBody7506L mLayoutBody7506L;
    private LayoutFunctionView7506 mLayoutFunction;

    private TextView mTvTime;
    private LayoutGears5_7506 mLlAirStrong;

    private ImageView mIvShoulder;
    private ImageView mIvBack;
    private ImageView mIvHand;
    private ImageView mIvLeg;
    private ImageView mIvAll;
    private List<ImageView> mIvAirPosition = new ArrayList<>();
    private String[] name = {"充气位置", "充气强度"};
    private MoveLayout mLayout;
    private ScrollView mScroll;
    private long mTime;
    private DownTimer mTimer;
    private final long INTERVAL = 1000L;
    private FraAirPresenter7506 mPresenter;
    private long mPauseTime;
    private boolean isPause;
    private ViewStub mView7506;
    private ViewStub mView7506L;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        inflate = inflater.inflate(R.layout.fragment_air_pressure_massage_7506, container, false);
        initView();
        return inflate;
    }


    private void initView() {
        PreferenceWrapper mSp = new PreferenceWrapper();
        int mChoose = Integer.parseInt(mSp.getStringValue("app", "1"));
        mIvShoulder = inflate.findViewById(R.id.fragment_air_shoulder);
        mIvBack = inflate.findViewById(R.id.fragment_mt_iv_back);
        if (mChoose == 4) {
            mIvBack.setImageResource(R.drawable.selector_gas_waist);
        }
        mIvHand = inflate.findViewById(R.id.fragment_mt_iv_hand);
        mIvLeg = inflate.findViewById(R.id.fragment_mt_iv_leg);
        mIvAll = inflate.findViewById(R.id.fragment_mt_iv_all);
        mScroll = inflate.findViewById(R.id.fragment_air_scoll);
        mLayout = inflate.findViewById(R.id.air_fragment_move_layout);
        mTvTime = inflate.findViewById(R.id.fragment_air_tv_time);
        mLayout.setData(name);
        mLayout.setScollView(mScroll);
        mLayout.setAction(this);
        mIvAirPosition.clear();
        mIvAirPosition.add(mIvShoulder);
        mIvAirPosition.add(mIvBack);
        mIvAirPosition.add(mIvHand);
        mIvAirPosition.add(mIvLeg);
        mIvAirPosition.add(mIvAll);


        mLlAirPosition = inflate.findViewById(R.id.linear_methods_to_select);
        mLlAirStrong = inflate.findViewById(R.id.rl_air_strong);

        mLlAirStrong.setType(3);
        if (!TextUtils.isEmpty(DataManager.getInst().getValue(CommmandNum.mStatusAirStrong))) {
            Log.e("setsetstring", DataManager.getInst().getValue(CommmandNum.mStatusAirStrong) + "");
            mLlAirStrong.setShow(Integer.valueOf(DataManager.getInst().getValue(CommmandNum.mStatusAirStrong)));
        }
        mView7506 = inflate.findViewById(R.id.layout_body_view);
        mView7506L = inflate.findViewById(R.id.layout_body_view_7506_l);
        if (DataManager.getInst().isIs7506L()) {
            if (mLayoutBody7506L == null) {
                mLayoutBody7506L = (LayoutBody7506L) mView7506L.inflate();
            }
        } else {
            if (mLayoutBody7506 == null) {
                mLayoutBody7506 = (LayoutBody7506) mView7506.inflate();
            }
        }
        mLayoutFunction = inflate.findViewById(R.id.fragment_air_layout_function);
        mPresenter = new FraAirPresenter7506(this);
        mTvTime.setOnClickListener(this);


        mIvShoulder.setOnClickListener(this);
        mIvBack.setOnClickListener(this);
        mIvHand.setOnClickListener(this);
        mIvLeg.setOnClickListener(this);
        mIvAll.setOnClickListener(this);
        initData();
    }

    public void initData() {
        mPresenter.getAirPosition(CommmandNum.mStatusAirShoulder, DataManager.getInst().getValue(CommmandNum.mStatusAirShoulder));
        mPresenter.getAirPosition(CommmandNum.mStatusAirSit, DataManager.getInst().getValue(CommmandNum.mStatusAirSit));
        mPresenter.getAirPosition(CommmandNum.mStatusAirHand, DataManager.getInst().getValue(CommmandNum.mStatusAirHand));
        mPresenter.getAirPosition(CommmandNum.mStatusAirLeg, DataManager.getInst().getValue(CommmandNum.mStatusAirLeg));
        mPresenter.getAirPosition(CommmandNum.mStatusAirAll, DataManager.getInst().getValue(CommmandNum.mStatusAirAll));
        mPresenter.getAirPosition(CommmandNum.mStatusAirStrong, DataManager.getInst().getValue(CommmandNum.mStatusAirStrong));
    }

    public void initTime(String min, String sec) {
        setTime(Integer.valueOf(min), Integer.valueOf(sec));
    }

    public void upDataTime(String min, String sec) {
        if (DataManager.getInst().isAdvanceRunning() || DataManager.getInst().isAutoRunning()) {
            if (!(min.equals("0") && sec.equals("0"))) {
                long time = (Integer.valueOf(min) * 60 + Integer.valueOf(sec));
                Log.e("Timetimetiem11122", mTime + "===" + time);
                if (Math.abs(time - mTime) > 30) {
                    Log.e("Timetimetiem11122", mTime + "===" + time);
                    setTime(Integer.valueOf(min), Integer.valueOf(sec));
                }
            } else {
                mTvTime.setText("00 : 00");
                mTime = 0;
            }

        } else {
            cancelTime();
            mTvTime.setText("00 : 00");
            mTime = 0;
        }


    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        mPresenter.onDestory();
        mLayoutFunction.onDestory();
        if (mLayoutBody7506 != null) {
            mLayoutBody7506.onDestory();
        }
        if (mLayoutBody7506L != null) {
            mLayoutBody7506L.onDestory();
        }
        cancelTime();

    }


    @Override
    public void onClick(View v) {
        int i = v.getId();
        if (i == R.id.fragment_air_tv_time) {
            if (CommonUtil.isFastClick()) {
                return;
            } else {

                showDialog();
            }

        } else if (i == R.id.fragment_air_shoulder) {
            if (CommonUtil.isFastClick()) {
                return;
            } else {
                sendCommond(CommmandNum.AIR_JIANB);

            }

        } else if (i == R.id.fragment_mt_iv_back) {
            if (CommonUtil.isFastClick()) {
                return;
            } else {
                sendCommond(CommmandNum.AIR_BEIB);
            }

        } else if (i == R.id.fragment_mt_iv_hand) {
            if (CommonUtil.isFastClick()) {
                return;
            } else {
                sendCommond(CommmandNum.AIR_SHOUB);
            }

        } else if (i == R.id.fragment_mt_iv_leg) {
            if (CommonUtil.isFastClick()) {
                return;
            } else {
                sendCommond(CommmandNum.AIR_TUIB);
            }

        } else if (i == R.id.fragment_mt_iv_all) {
            if (CommonUtil.isFastClick()) {
                return;
            } else {
                sendCommond(CommmandNum.AIR_ALL);
            }

        }
    }


    @Override
    public void onChoose(int position) {
        mLayout.setTectColor(position);
        switch (position) {
            case 0:
                mLlAirPosition.setVisibility(View.VISIBLE);
                mLlAirStrong.setVisibility(View.GONE);
                break;
            case 1:
                mLlAirPosition.setVisibility(View.GONE);
                mLlAirStrong.setVisibility(View.VISIBLE);
                break;
        }

    }

    private void setPosition(int position, boolean isSelect) {
        Log.e("pisitionnnn", position + "");
        mIvAirPosition.get(position).setSelected(isSelect);
    }

    @Override
    public void getTime(String time) {
        sendTime(time);
    }

    @Override
    public void getAirPosition(int position, int isRun) {
        setPosition(position, isRun == 1);
    }

    @Override
    public void getAirStrong(int strong) {
        mLlAirStrong.setShow(strong);
    }

    public void pauseTime() {
        if (mTimer != null) {
            mTimer.pause();
        }

    }

    public void reStarTime() {
        if (mTimer != null) {
            mTimer.resume();
        }


    }

    public void cancelTime() {
        if (mTimer != null) {
            mTimer.cancel();
        }


    }

    public void setTime(int min, int sec) {
        cancelTime();
        if (DataManager.getInst().isAdvanceRunning() || DataManager.getInst().isAutoRunning()) {
            mTimer = new DownTimer();//实例化
            mTimer.setTotalTime(min * 60 * 1000 + sec * 1000);//设置毫秒数
            mTimer.setIntervalTime(1000);//设置间隔数
            mTimer.setTimerLiener(new DownTimer.TimeListener() {
                @Override
                public void onFinish() {
                    mTvTime.setText("00 : 00");
                    mTime = 0;
                    CloseAdvance.getInst().noticeonClose();
                }

                @Override
                public void onInterval(long time) {
                    long showTime = time / 1000;
                    mTime = showTime;
                    if (showTime <= 59) {
                        mTvTime.setText(String.format("00:%02d", showTime));
                    } else {
                        mTvTime.setText(String.format("%02d:%02d", showTime / 60, showTime % 60));
                    }
                    if (!DataManager.getInst().isAdvanceRunning() && DataManager.getInst().isAutoRunning()) {
                        mTimer.cancel();
                        mTvTime.setText("00 : 00");
                        mTime = 0;
                    }
                }
            });
            mTimer.start();
        } else {
            mTimer.cancel();
            mTvTime.setText("00 : 00");
            mTime = 0;
        }

    }

}
